//
//  HudAnimation.swift
//  Response
//
//  Created by JunMing on 2020/1/15.
//  Copyright © 2020 JMZhao. All rights reserved.
//

import UIKit

class HudAnimation: UIView,SheetCompProtocol {
    var item: SheetItem?
    private var animation = LoadAnimation()
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(animation)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func updateView() {
        self.tag = 10101
        self.layer.cornerRadius = 10
        self.layer.masksToBounds = true
        self.backgroundColor = UIColor.RGB(200, 200, 200)
        animation.startAni()
    }
    
    func remove(_ backView:SheetbackView) {
        LoadAnimation.hideAnimation(backView)
        UIView.animate(withDuration: 0.3, animations: {
            backView.alpha = 0
        }) { _ in
            backView.removeFromSuperview()
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        animation.frame = bounds
    }
}

class LoadAnimation: UIView {
    let topLayer = CAShapeLayer()
    let bottomLayer = CAShapeLayer()
    let leftLayer = CAShapeLayer()
    let rightLayer = CAShapeLayer()
    var animating = false
    
    private let h_height:CGFloat = 60.0
    private let r_radius:CGFloat = 60.0/7
    private let translatLen:CGFloat = 60.0/7
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        layer.addSublayer(topLayer)
        layer.addSublayer(leftLayer)
        layer.addSublayer(bottomLayer)
        layer.addSublayer(rightLayer)
        setupSubLayers()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupSubLayers() {
        let centerLine = h_height/2
        let radius = r_radius
        
        let topPoint = CGPoint(x: centerLine, y: radius)
        let topcolor = UIColor.RGB(90, 200, 200)
        configLayer(topPoint, topcolor, topLayer)
        
        let leftPoint = CGPoint(x: radius, y: centerLine)
        configLayer(leftPoint, UIColor.RGB(250, 85, 78), leftLayer)
        
        let bottomPoint = CGPoint(x: centerLine, y: h_height - radius)
        configLayer(bottomPoint, UIColor.RGB(92, 201, 105), bottomLayer)
        
        let rightPoint = CGPoint(x: h_height - radius, y: centerLine)
        configLayer(rightPoint, UIColor.RGB(253, 175, 75), rightLayer)
    }
    
    public static func hideAnimation(_ superView:UIView) {
        for view in superView.subviews {
            if view.isKind(of: self) {
                let ani = view as! LoadAnimation
                ani.removeAni()
            }else if view.tag == 10101 {
                UIView.animate(withDuration: 0.5, animations: {
                    view.alpha = 0.0
                }) { (finished) in
                    view.removeFromSuperview()
                }
            }
        }
    }
    
    func startAni() {
        animating = true
        addTranslationAniToLayer(topLayer, x: 0, y: translatLen)
        addTranslationAniToLayer(leftLayer, x: translatLen, y: 0)
        addTranslationAniToLayer(bottomLayer, x: 0, y: -translatLen)
        addTranslationAniToLayer(rightLayer, x: -translatLen, y: 0)
        addRotationAniToLayer(layer)
    }
    
    private func configLayer(_ center:CGPoint,_ color:UIColor,_ layer:CAShapeLayer) {
        layer.opacity = 1.0;
        layer.fillColor = color.cgColor;
        layer.frame = CGRect(x: center.x - r_radius, y: center.y - r_radius, width: r_radius * 2, height: r_radius * 2)
        let bezier = UIBezierPath(arcCenter: CGPoint(x: r_radius, y: r_radius), radius: r_radius, startAngle: 0, endAngle: CGFloat(.pi * 2.0), clockwise: true)
        layer.path = bezier.cgPath
    }
    
    private func removeAni() {
        UIView.animate(withDuration: 0.5, animations: {
            self.alpha = 0.0
        }) { (finished) in
            self.topLayer.removeAllAnimations()
            self.leftLayer.removeAllAnimations()
            self.bottomLayer.removeAllAnimations()
            self.rightLayer.removeAllAnimations()
            self.layer.removeAllAnimations()
            self.animating = false
        }
    }
    
    private func addTranslationAniToLayer(_ layer:CALayer,x:CGFloat,y:CGFloat) {
        let translationKeyframeAni = CAKeyframeAnimation(keyPath: "transform")
        translationKeyframeAni.duration = 1.3;
        translationKeyframeAni.repeatCount = HUGE;
        translationKeyframeAni.isRemovedOnCompletion = false;
        translationKeyframeAni.fillMode = CAMediaTimingFillMode.forwards;
        translationKeyframeAni.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.linear)
        let fromValue = NSValue(caTransform3D: CATransform3DMakeTranslation(0, 0, 0))
        let toValue = NSValue(caTransform3D: CATransform3DMakeTranslation(x, y, 0))
        translationKeyframeAni.values = [fromValue, toValue, fromValue, toValue, fromValue];
        layer.add(translationKeyframeAni, forKey: "translationKeyframeAni")
    }
    
    private func addRotationAniToLayer(_ layer:CALayer) {
        let rotationAni = CABasicAnimation(keyPath: "transform.rotation.z")
        rotationAni.fromValue = 0
        rotationAni.toValue = CGFloat(.pi * 2.0)
        rotationAni.duration = 1.3;
        rotationAni.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.linear)
        rotationAni.repeatCount = HUGE;
        rotationAni.fillMode = CAMediaTimingFillMode.forwards
        rotationAni.isRemovedOnCompletion = false
        layer.add(rotationAni, forKey: "rotationAni")
    }
}

